from ..models.inherit.res_partner_inherit_for_customer import REL_TYPE, ADDRESS_TYPE
from ...basic_configuration.models.inherit.res_partner import fulfill_name


class AddressFunctions(object):
    """
    Model:
        满足地址的所有算法逻辑

    Param:
        - env: odoo env

    Function:
        - get_address: 检验地址，有则获取，没有则创建
    """

    def __init__(self, env):
        self.env = env

    def get_address(self, customer, first_name, last_name, mobile, address_type, company_name,
                    country, state, city, county, street, postal_code, email):
        """
        检验地址，有则获取，没有则创建(res.partner)

        :param customer 客户信息(rp.customer)
        :param first_name 名
        :param last_name 姓
        :param mobile 手机号
        :param address_type 地址类型 ADDRESS_TYPE
        :param company_name 公司名称
        :param country 国家(res.country)
        :param state 省(res.country.state)
        :param city 市(res.city)
        :param county 区(res.county)
        :param street 街道
        :param postal_code 邮编(rp.postal.code)

        @return address(res.partner)
        """
        rcd = self.env['res.partner'].search([('customer_address_id', '=', customer.id),
                                              ('rel_type', '=', REL_TYPE[0][0]),
                                              ('first_name', '=', first_name),
                                              ('last_name', '=', last_name),
                                              ('mobile', '=', mobile),
                                              ('address_type', '=', address_type),
                                              ('contact_company_name', '=', company_name),
                                              ('country_id', '=', country.id),
                                              ('state_id', '=', state.id),
                                              ('city_id', '=', city.id),
                                              ('county_id', '=', county.id if county else False),
                                              ('street', '=', street),
                                              ('postal_code_id', '=', postal_code.id),
                                              ('email', '=', email)])
        if rcd:
            return rcd
        else:
            address = self.env['res.partner'].create([{'customer_address_id': customer.id,
                                                      'rel_type': REL_TYPE[0][0],
                                                      'first_name': first_name,
                                                      'last_name': last_name,
                                                      'name': fulfill_name(first_name, last_name),
                                                      'mobile': mobile,
                                                      'address_type': address_type,
                                                      'contact_company_name': company_name if address_type == ADDRESS_TYPE[1][0] else False,
                                                      'country_id': country.id,
                                                      'state_id': state.id,
                                                      'city_id': city.id,
                                                      'county_id': county.id if county else False,
                                                      'street': street,
                                                      'postal_code_id': postal_code.id,
                                                      'email': email}])
            return address
